﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.Script
{
    public class TabContainerScriptObject: PartScriptObject
    {
        private WeakReference _partContainer;
        public TabContainerScriptObject(TabContainerPart partContainer)
            : base(partContainer)
        {
            _partContainer = new WeakReference(partContainer);
        }

        public object getObject(string name)
        {
            TabContainerPart partContainer = _partContainer.Target as TabContainerPart;
            if (partContainer == null)
            {
                return null;
            }

            UI.Part part = partContainer.GetChildPart(name, true);
            if (part != null)
            {
                return part.GetScriptObj();
            }
            return null;
        }

        /// <summary>
        /// 选中选项卡
        /// </summary>
        /// <param name="newIndex"></param>
        public void selectTab(int newIndex)
        {
            TabContainerPart partContainer = _partContainer.Target as TabContainerPart;
            if (partContainer == null)
            {
                return;
            }
            // 将事件发送到EventBus
            Common.EventBus.Instance.Publish(new Event.TabChangeEvent(partContainer.ActiveTabIndex, newIndex, "", partContainer.Name));
        }
    }
}